<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户名称" prop="lesseeName">
        <el-input
          v-model="queryParams.lesseeName"
          placeholder="请输入商户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人名称" prop="signName">
        <el-input
          v-model="queryParams.signName"
          placeholder="请输入法人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人身份证号" prop="identityCard">
        <el-input
          v-model="queryParams.identityCard"
          placeholder="请输入法人身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="联系方式" prop="lesseePhone">-->
<!--        <el-input-->
<!--          v-model="queryParams.lesseePhone"-->
<!--          placeholder="请输入联系方式"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="注册地址" prop="lesseeAddr">
        <el-input
          v-model="queryParams.lesseeAddr"
          placeholder="请输入注册地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="付费模式" prop="lesseePolicy">-->
<!--        <el-input-->
<!--          v-model="queryParams.lesseePolicy"-->
<!--          placeholder="请输入付费模式"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="审批通过时间" prop="approvalTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.approvalTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择审批通过时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="到期时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择到期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['lessee:lessee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lessee:lessee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lessee:lessee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['lessee:lessee:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="lesseeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商户编号" align="center" prop="lesseeId" />
      <el-table-column label="商户名称" align="center" prop="lesseeName" />
      <el-table-column label="法人名称" align="center" prop="signName" />
      <el-table-column label="法人身份证号" align="center" prop="identityCard" />
      <el-table-column label="联系方式" align="center" prop="lesseePhone" />
      <el-table-column label="注册地址" align="center" prop="lesseeAddr" />
<!--      prop="lesseePolicy"-->
      <el-table-column label="付费模式" align="center" prop="lesseePolicy">
      <template slot-scope="scope">
        <span v-if="scope.row.lesseePolicy==1" style="color: green">年费模式</span>
        <span v-if="scope.row.lesseePolicy==2" style="color: red">抽成模式</span>
      </template>
      </el-table-column>
      <el-table-column label="审批通过时间" align="center" prop="approvalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <span v-if="scope.row.state==1" style="color: green">审核通过</span>
          <span v-if="scope.row.state==2" style="color: red">审核未通过</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lessee:lessee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lessee:lessee:remove']"
          >删除</el-button>
<!--          入驻按钮-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            v-if="scope.row.state!=2"-->
<!--            @click="opendilag(scope.row)"-->
<!--          >点击入驻</el-button>-->
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.state!=2"
            @click="openPay(scope.row)"
          >点击入驻</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      {{this.form}}
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户名称" prop="lesseeName">
          <el-input v-model="form.lesseeName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="法人名称" prop="signName">
          <el-input v-model="form.signName" placeholder="请输入法人名称" />
        </el-form-item>
        <el-form-item label="法人身份证号" prop="identityCard">
          <el-input v-model="form.identityCard" placeholder="请输入法人身份证号" />
        </el-form-item>
        <el-form-item label="联系方式" prop="lesseePhone">
          <el-input v-model="form.lesseePhone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="注册地址" prop="lesseeAddr">
          <el-input v-model="form.lesseeAddr" placeholder="请输入注册地址" />
        </el-form-item>
        <el-form-item label="付费模式" prop="lesseePolicy">
          <el-select v-model="form.lesseePolicy" placeholder="请选择付费模式">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付渠道" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付渠道">
            <el-option
              v-for="item in pay"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

<!--        <el-form-item label="审批通过时间" prop="approvalTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.approvalTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择审批通过时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="到期时间" prop="endTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.endTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择到期时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="状态" prop="state">-->
<!--          <el-input v-model="form.state" placeholder="请输入状态" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    入驻上传资料对话框-->
    <!--    编辑资料-->
    <el-dialog title="完善租户门店资料" :visible.sync="dialogFormVisible">
      <el-steps :space="200" :active="active" finish-status="success">
        <el-step title="租户身份查验"></el-step>
        <el-step title="门店信息"></el-step>
        <el-step title="付费模式选择"></el-step>
        <el-step title="宣传视频"></el-step>
      </el-steps>

      <!--      编号0的租户身份查验-->

      <!--      编号1的租户信息-->
      <el-form v-show="active==1" ref="form" :model="lesseeForm" :rules="rules" label-width="80px">
        <el-form-item label="商户编号" prop="lesseeId">
          <el-input v-model="lesseeForm.lesseeId" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="storeName">
          <el-input v-model="lesseeForm.storeName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="店铺电话" prop="storePhoto">
          <el-input v-model="lesseeForm.storePhoto" placeholder="请输入店铺电话" />
        </el-form-item>
        <el-form-item label="店铺地址" prop="storeAddr">
          <el-input v-model="lesseeForm.storeAddr" placeholder="请输入店铺地址" />
        </el-form-item>
        <el-form-item label="门面图" prop="storePic">
          <image-upload v-model="lesseeForm.storePic"/>
        </el-form-item>
        <el-form-item label="店内图" prop="storeInsidePic">
          <image-upload v-model="lesseeForm.storeInsidePic"/>
        </el-form-item>
      </el-form>

      <el-button style="margin-top: 12px;" @click="next">下一步</el-button>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import { listLessee, getLessee, delLessee, addLessee, updateLessee } from "@/api/lessee/lessee";

export default {
  name: "Lessee",
  data() {
    return {

      dialogFormVisible:false,
      active: 0,
      lesseeForm:{},

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租户表格数据
      lesseeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lesseeName: null,
        signName: null,
        identityCard: null,
        lesseePhone: null,
        lesseeAddr: null,
        lesseePolicy: null,
        approvalTime: null,
        endTime: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

      //下拉框
      options: [{
        value: 1,
        label: '年费模式'
      }, {
        value: 2,
        label: '抽成模式(0.38%每笔)',
        disable: true
      }],

      pay: [{
        value: 1,
        label: '微信支付'
      }, {
        value: 2,
        label: '支付宝支付'
      }],
      value: ''
    };
  },
  //监听事件
  // watch:{
  //   "form.lesseeAddr":function (newValue){
  //     if (this.form.lesseePolicy== 1 && newValue== 1){
  //       //发起微信支付
  //       alert("111")
  //     }
  //   },
  // },
  created() {
    this.getList();
  },
  methods: {

    //步骤条函数
    next() {
      if (this.active++ > 2) this.active = 0;
    },
    //编辑资料对话框函数
    opendilag(row){
      this.dialogFormVisible=true;
      this.lesseeForm=row;
    },


    /** 查询租户列表 */
    getList() {
      this.loading = true;
      listLessee(this.queryParams).then(response => {
        this.lesseeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lesseeId: null,
        lesseeName: null,
        signName: null,
        identityCard: null,
        lesseePhone: null,
        lesseeAddr: null,
        lesseePolicy: null,
        createTime: null,
        approvalTime: null,
        endTime: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lesseeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const lesseeId = row.lesseeId || this.ids
      getLessee(lesseeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.lesseeId != null) {
            updateLessee(this.form).then(response => {
              if (this.form.lesseePolicy==1 && this.form.payType ==1){
                alert("111")
              }
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLessee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lesseeIds = row.lesseeId || this.ids;
      this.$modal.confirm('是否确认删除租户编号为"' + lesseeIds + '"的数据项？').then(function() {
        return delLessee(lesseeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lessee/lessee/export', {
        ...this.queryParams
      }, `lessee_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
